package com.zeng.springboot03web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/jdbc")
public class JdbcController {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @GetMapping("list")
    public List<Map<String,Object>> userList(){
        String sql = "select * from books";
        List<Map<String,Object>> maps = jdbcTemplate.queryForList(sql);
        return maps;
    }

    // 好像无驼峰命名法
    @GetMapping("/add")
    public String addUser(){
        String sql = "insert into books(bookName,bookCounts,detail)"+
                "values('jdbc','0','底层')";
        jdbcTemplate.update(sql);
        return "addOk";
    }

    // 修改信息
    @GetMapping("update/{id}")
    public String updateUser(@PathVariable("id")int id){
        String sql = "update books set bookName=?,bookCounts=?,detail=? where bookID="+id;
        Object[] object = new Object[3];
        object[0] = "jdbc update";
        object[1] = 0;
        object[2] = "修改";
        jdbcTemplate.update(sql,object);
        return "Ok";
    }

    @GetMapping("delete/{id}")
    public String delete(@PathVariable("id") int id){
        String sql = "delete from books where bookID=?";
        jdbcTemplate.update(sql,id);
        return "ok";
    }
}
